<html>
<head><meta charset="utf-8"><title>Serializing a struct with serde · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serializing.20a.20struct.20with.20serde.html">Serializing a struct with serde</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="231312202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serializing%20a%20struct%20with%20serde/near/231312202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nuno Martins <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serializing.20a.20struct.20with.20serde.html#231312202">(Mar 22 2021 at 13:46)</a>:</h4>
<p>Hi all. I'm creating my first project in Rust and facing some difficulties. This is my code and I will describe my problem below it.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">client</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">reqwest</span>::<span class="n">blocking</span>::<span class="n">Client</span>::<span class="n">new</span><span class="p">();</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">client</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="s">"http://myapi.com/account"</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">bearer_auth</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">.</span><span class="n">api_token</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">send</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">unwrap</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">text</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">unwrap</span><span class="p">();</span><span class="w"></span>

<span class="fm">println!</span><span class="p">(</span><span class="s">"raw: {}"</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">);</span><span class="w"></span>

<span class="kd">let</span><span class="w"> </span><span class="n">account</span>: <span class="nc">Account</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">serde_json</span>::<span class="n">from_str</span><span class="p">(</span><span class="o">&amp;</span><span class="n">res</span><span class="p">).</span><span class="n">unwrap</span><span class="p">();</span><span class="w"></span>

<span class="cp">#[derive(Deserialize, Debug)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">Account</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span>: <span class="kt">u32</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">b</span>: <span class="kt">u32</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">c</span>: <span class="nb">String</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>I'm using reqwest to perform a get and than using serde to serialize it to a struct. This is basically failing because the response is something like this:</p>
<div class="codehilite" data-code-language="JSON"><pre><span></span><code><span class="p">{</span>
    <span class="nt">"account"</span><span class="p">:</span> <span class="p">{</span>
        <span class="nt">"a"</span><span class="p">:</span> <span class="mi">10</span><span class="p">,</span>
        <span class="nt">"b"</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
        <span class="nt">"c"</span><span class="p">:</span> <span class="s2">"some text"</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>Since this is a crate to be used by other people I would like it to expose a nice struct, like the one I declared. Is there any way to remove/ignore that "account" field in order to serde serialize the response directly to my struct? BTW, probably the code is not very good but I'm starting in Rust. Any comments are appreciated.</p>



<a name="231312507"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serializing%20a%20struct%20with%20serde/near/231312507" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serializing.20a.20struct.20with.20serde.html#231312507">(Mar 22 2021 at 13:48)</a>:</h4>
<p><span class="user-mention" data-user-id="399668">@Nuno Martins</span> you may get faster feedback on the official or community discord, or on <a href="http://users.rust-lang.org">users.rust-lang.org</a>. Zulip is mostly used by contributors to rust-lang/rust itself - it's fine to ask for help here, it just may not be very fast</p>



<a name="231313359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serializing%20a%20struct%20with%20serde/near/231313359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serializing.20a.20struct.20with.20serde.html#231313359">(Mar 22 2021 at 13:54)</a>:</h4>
<p><a href="https://serde.rs/container-attrs.html#transparent">https://serde.rs/container-attrs.html#transparent</a></p>



<a name="231317454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serializing%20a%20struct%20with%20serde/near/231317454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serializing.20a.20struct.20with.20serde.html#231317454">(Mar 22 2021 at 14:20)</a>:</h4>
<p>Either you use a wrapper or use <code>serde_json::Value</code> to access <code>value["account"]</code> and then deserialize to a struct. There was an issue about an annotation that could strip a prefix like that but I don't that has been implemented.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>